package org.jeecg.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.business.entity.BussOutletQuote;
import org.jeecg.modules.business.entity.BussOutletQuotePackage;
import org.jeecg.modules.business.entity.BussOutletQuoteProduct;
import org.jeecg.modules.business.mapper.BussOutletQuoteMapper;
import org.jeecg.modules.business.service.IBussOutletQuotePackageService;
import org.jeecg.modules.business.service.IBussOutletQuoteProductService;
import org.jeecg.modules.business.service.IBussOutletQuoteService;
import org.jeecg.modules.business.vo.BussOutletQuoteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @Description: 出口业务报价单
 * @Author: jeecg-boot
 * @Date:   2024-05-31
 * @Version: V1.0
 */
@Service
public class BussOutletQuoteServiceImpl extends ServiceImpl<BussOutletQuoteMapper, BussOutletQuote> implements IBussOutletQuoteService {


    @Autowired
    private IBussOutletQuotePackageService bussOutletQuotePackageService;

    @Autowired
    private IBussOutletQuoteProductService iBussOutletQuoteProductService;

    @Autowired
    private BussOutletQuoteMapper bussOutletQuoteMapper;
    /**
     * 保存出口出口报价单
     * @param outletQuoteVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOutletQuote(BussOutletQuoteVo outletQuoteVo) {
        this.save(outletQuoteVo);
        this.initData(outletQuoteVo);
        bussOutletQuotePackageService.saveBatch(outletQuoteVo.getErpGoodsPackageList());
        iBussOutletQuoteProductService.saveBatch(outletQuoteVo.getProductQuotationList());
    }

    /**
     * 修改
     * @param outletQuoteVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editOutletQuote(BussOutletQuoteVo outletQuoteVo) {
        this.updateById(outletQuoteVo);
        this.bussOutletQuotePackageService.remove(new LambdaQueryWrapper<BussOutletQuotePackage>()
                .eq(BussOutletQuotePackage::getOutQuoteId, outletQuoteVo.getId()));
        this.iBussOutletQuoteProductService.remove(new LambdaQueryWrapper<BussOutletQuoteProduct>()
                .eq(BussOutletQuoteProduct::getOutQuoteId, outletQuoteVo.getId()));
        this.initData(outletQuoteVo);
        bussOutletQuotePackageService.saveBatch(outletQuoteVo.getErpGoodsPackageList());
        iBussOutletQuoteProductService.saveBatch(outletQuoteVo.getProductQuotationList());
    }

    @Override
    public BussOutletQuoteVo queryByReviewId(String id) {
        BussOutletQuoteVo bussOutletQuoteVo = bussOutletQuoteMapper.queryByReviewId(id);
        if (bussOutletQuoteVo!=null) {
            bussOutletQuoteVo.setErpGoodsPackageList(bussOutletQuotePackageService.selectByMainId(bussOutletQuoteVo.getId()));
            bussOutletQuoteVo.setProductQuotationList(iBussOutletQuoteProductService.selectByMainId(bussOutletQuoteVo.getId()));
        }else {
            bussOutletQuoteVo = new BussOutletQuoteVo();
        }
        return bussOutletQuoteVo;
    }

    private void initData(BussOutletQuoteVo outletQuoteVo){
        if(!CollectionUtils.isEmpty(outletQuoteVo.getErpGoodsPackageList()))
        outletQuoteVo.getErpGoodsPackageList().stream().forEach(item->item.setOutQuoteId(outletQuoteVo.getId()));
       if(!CollectionUtils.isEmpty(outletQuoteVo.getProductQuotationList()))
        outletQuoteVo.getProductQuotationList().stream().forEach(item->item.setOutQuoteId(outletQuoteVo.getId()));
    }


}
